package com.accesshollywood.GL;

import java.awt.Point;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import org.lwjgl.util.vector.Vector3f;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

public class Zone extends AnimObject {
	public static boolean	ANIMATE_RELAYOUT	= true;

	public MasterControl	masterControl;

	public enum TYPE {
		COUPLE, PEOPLE, BUTTON, FINAL, HIGHLIGHT
	};

	public float		WIDTH;
	public float		HEIGHT;
	public float		marginRatio	= 10.0f;
	public float		margin;
	public int			cols;
	public int			rows;
	public TYPE			zoneType;

	public FileSeqID	fileID;

	public int			currID		= 0;

	public Zone(float X, float Y, float W, float H, TYPE type) {
		super();
		posCurr.x = X;
		posCurr.y = Y;
		WIDTH = W;
		HEIGHT = H;
		zoneType = type;
	}

	public void reset() {
		children = new ArrayList<AnimObject>();
	}

	public void addPlaceholders(int N) {
		for (int i = 0; i < N; i++) {
			addMember(new Couple());
		}
	}

	public void addButtons() {

	}

	public void loadFaceArray(File photoPath) {
		String[] photoFiles = photoPath.list();
		for (int i = 0; i < photoFiles.length; i++) {
			photoFiles[i] = photoPath.getAbsolutePath() + "\\" + photoFiles[i];
			try {
				String ext = photoFiles[i].substring(photoFiles[i].lastIndexOf(".") + 1, photoFiles[i].length());
				if (ext.equals("png")) {
					Texture texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream(photoFiles[i]));
					Picture newPicture = new Picture(texture);
					newPicture.masterControl = masterControl;
					newPicture.idGL = currID++;
					masterControl.addMemberToZone(newPicture, this);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		masterControl.setAsReady();
		// ((Picture)children.get(3)).onClick();
	}

	public void addMember(AnimObject newMember) {
		newMember.posCurr.x = MatchUpGLMain.SCREEN_WIDTH / 2.0f;
		newMember.posCurr.y = MatchUpGLMain.SCREEN_HEIGHT / 2.0f;

		children.add(newMember);
		newMember.parent = this;

		reLayout(this != masterControl.peopleZone);
	}

	public void removeSelected() {
		ArrayList<Picture> removeUs = new ArrayList<Picture>();
		for (int i = 0; i < children.size(); i++) {
			Picture currPicture = (Picture) children.get(i);
			if (currPicture.isSelected()) {
				removeUs.add(currPicture);
			}
		}
		for (int i = 0; i < removeUs.size(); i++) {
			children.remove(removeUs.get(i));
		}
		reLayout(true);
	}
	
	public void removeAll(){
		children.clear();
		reLayout(true);
	}

	public void reLayout(boolean animate) {
		int numKids = children.size();
		if (numKids > 0) {
			Point newLayout = findBestLayout(numKids, WIDTH / HEIGHT);
			cols = newLayout.x;
			rows = newLayout.y;
			float imageWidth = WIDTH / (cols * (marginRatio + 1) + marginRatio);
			float imageHeight = HEIGHT / (rows * (marginRatio + 1) + marginRatio);
			float margin = Math.min(marginRatio * imageWidth, marginRatio * imageHeight);
			float temp = children.get(0).scaleCurr.x;
			float Xscale = imageWidth / (children.get(0).getWidth() / children.get(0).scaleCurr.x);
			float Yscale = imageHeight / (children.get(0).getHeight() / children.get(0).scaleCurr.y);
			int i2, j2 = 0;
			float scaler = Math.min(Xscale, Yscale);
			for (int i = 0; i < children.size(); i++) {
				Picture currPicture = (Picture) children.get(i);
				i2 = i % cols;
				j2 = (int) Math.floor(i / cols);
				float newX = (i2 + 1) * margin + i2 * imageWidth + imageWidth / 2.0f;
				float newY = (j2 + 1) * margin + j2 * imageHeight + imageHeight / 2.0f;

				if (animate && (!(currPicture instanceof Match) && !(currPicture instanceof NoMatch))) {
					Keyframe newKey = new Keyframe();

					newKey.setPosXYZ(new Vector3f(newX, newY, 0));
					newKey.setScaleXYZ(new Vector3f(Math.min(1.0f, scaler), Math.min(1.0f, scaler), 1.0f));
					newKey.setFrames(20);
					currPicture.scaleBase = scaler;
					currPicture.addKeyframe(newKey);
					currPicture.homeKey = newKey.clone();
					currPicture.processNextKeyframe(!Keyframe.POLITE_KEYFRAME);
					currPicture.unselectMe();
				} else {
					currPicture.posCurr.x = newX;
					currPicture.posCurr.y = newY;
					currPicture.scaleCurr.x = (float) Math.min(1.0, scaler);
					currPicture.scaleCurr.y = (float) Math.min(1.0, scaler);
					currPicture.homeKey = new Keyframe();
					currPicture.homeKey.setPosXYZ(currPicture.posCurr);
					currPicture.homeKey.setRotXYZ(currPicture.rotCurr);
					currPicture.homeKey.setScaleXYZ(currPicture.scaleCurr);
					currPicture.homeKey.setAlpha(currPicture.alphaCurr);
					currPicture.scaleBase = scaler;
				}
			}
		}
	}

	public Point findBestLayout(int N, float targetAspect) {
		float closestMatch = 9999999;
		int closestID = 0;
		ArrayList<Integer> roots = new ArrayList<Integer>();
		for (int i = 1; i <= N; i++) {
			// if(N%i == 0){
			roots.add((int) Math.ceil(N / i));
			Picture currPicture = (Picture) children.get(i - 1);
			float W = currPicture.getWidth();
			float H = currPicture.getHeight();
			float a = i * W;
			float b = roots.get(i - 1) * H;
			float currAspect = a / b;
			float diff = Math.abs(currAspect - targetAspect);
			if (diff < closestMatch) {
				closestMatch = diff;
				closestID = i;
			}
			// }else{
			// roots.push(-1);
			// }
		}
		return (new Point(closestID, roots.get(closestID - 1)));
	}

}
